﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace Exercise18
{
    internal class StrFun
    {
        User User;
        public int OutNum;
        public User GetResult(string Instring)
        {
            User = new User(false,"Pass");            
            if (string.IsNullOrEmpty(Instring))     { User.bIsNull = true; User.StrMsg = "is null ";    return User; }
            if (!int.TryParse(Instring,out OutNum)) { User.bIsNull = true; User.StrMsg = "No a number"; return User; }
            OutNum = RecursionFun(OutNum);
            return User;
        }
        /// <summary>
        /// 一个简单的递归
        /// </summary>
        /// <param name="Num"></param>
        /// <returns></returns>
        private int RecursionFun(int Num)
        {            
            if (Num < 0)             { return 0; }
            if (Num == 1|| Num == 2) { return 1; }
            return RecursionFun(Num - 1) + RecursionFun(Num - 2);
        }
        /// <summary>
        /// 通过空间换时间
        /// </summary>
        /// <param name="Num"></param>
        /// <returns></returns>
        private int RecursionFun1(int Num)
        {
            int[] Numbers = new int[Num];
            Numbers[0] = 1;
            Numbers[1] = 1;
            for (int i = 2; i < Num; i++) {
                Numbers[i] = Numbers[i - 1] + Numbers[i - 2];
            }
            return Numbers[Num - 1];
        }
    }
}
